

let lines = `	// meshes: salivamesh
	shader "textures/monster/gob2.png"

	numverts 14
	vert 0 ( 0.6911731958 0.0044568777 ) 5 1
	vert 1 ( 1 0 ) 4 1
	vert 2 ( 0.6392842531 0.9973664284 ) 3 1
	vert 3 ( 1 1 ) 2 1
	vert 4 ( 0.004455179 0.0995602608 ) 8 2
	vert 5 ( 0.4410413802 0.0376555324 ) 6 2
	vert 6 ( 0 0.7592910528 ) 10 2
	vert 7 ( 0.263348937 0.8913338184 ) 0 2
	vert 8 ( 0.6911731958 0.0044568777 ) 15 1
	vert 9 ( 0.6392842531 0.9973664284 ) 14 1
	vert 10 ( 0.4410413802 0.0376555324 ) 16 2
	vert 11 ( 0.004455179 0.0995602608 ) 18 2
	vert 12 ( 0 0.7592910528 ) 20 2
	vert 13 ( 0.263348937 0.8913338184 ) 12 2

	numtris 12
	tri 0 2 1 0
	tri 1 3 1 2
	tri 2 6 5 4
	tri 3 7 5 6
	tri 4 7 0 5
	tri 5 2 0 7
	tri 6 9 8 1
	tri 7 3 9 1
	tri 8 12 11 10
	tri 9 13 12 10
	tri 10 13 10 8
	tri 11 9 13 8

	numweights 22
	weight 0 42 0.883179605 ( 2.3967983723 0.3203794658 -2.0581412315 )
	weight 1 43 0.1168204024 ( 1.5741927624 4.0278301239 3.003847599 )
	weight 2 42 1 ( 0.2083637416 -0.1439549923 -0.0211572647 )
	weight 3 42 1 ( 1.3220168352 -0.1327016354 -0.2023601532 )
	weight 4 43 1 ( -0.0339821279 -0.0598594584 -0.2053284645 )
	weight 5 43 1 ( 1.3098859787 0.1579706669 0.2332735062 )
	weight 6 42 0.0943331346 ( 2.1932461262 3.9681742191 -0.8029294014 )
	weight 7 43 0.9056668878 ( 1.9060581923 0.3894642591 1.7486357689 )
	weight 8 42 0.434630841 ( 2.7076961994 3.703653574 -4.6259832382 )
	weight 9 43 0.5653691292 ( 2.37631464 0.7263452411 5.5716896057 )
	weight 10 42 0.7020052075 ( 2.956643343 0.8846018314 -4.9596524239 )
	weight 11 43 0.2979947925 ( 2.2105014324 3.5515060425 5.9053587914 )
	weight 12 42 0.9204549193 ( -1.9800709486 0.3203794658 -2.0581412315 )
	weight 13 43 0.0795451254 ( -2.7556607723 3.3880236149 3.003847599 )
	weight 14 42 1 ( -0.9052894115 -0.1327016354 -0.2023601532 )
	weight 15 43 1 ( -1.3834183216 -0.24000898 0.2332735062 )
	weight 16 42 0.118848756 ( -1.7765187025 3.9681742191 -0.8029294014 )
	weight 17 43 0.8811512589 ( -2.021064043 -0.1908321083 1.7486357689 )
	weight 18 42 0.4651285112 ( -2.2909686565 3.703653574 -4.6259832382 )
	weight 19 43 0.534871459 ( -2.5686552525 -0.0043547237 5.5716896057 )
	weight 20 42 0.7337136865 ( -2.5399158001 0.8846018314 -4.9596524239 )
	weight 21 43 0.2662863135 ( -3.2270145416 2.7480244637 5.9053587914 )`;


function parseMd5Mesh(lines) {

    let info = {
        imagePath:'',

        numVerts:0,
        verts:[],

        numTris:0,
        tris:[],

        numWeights:0,
        weights:[],
    };

    let len = lines.length;
    for (let i = 0; i < len; ++i){
        let line = lines[i];
        let firstWord = getFirstWord(line.trim());
        switch (firstWord) {
            case "shader":
                // image name
                info.imagePath = getImagePath(line);
                break;
            case "numverts":
                info.numVerts = getNumVerts(line);
                break;
            case "vert":
                let vertInfo = getVertData(line);
                if (vertInfo){
                    info.verts.push(vertInfo);
                }
                break;
            case "numtris":
                info.numTris = getNumTris(line);
                break;
            case "tri":
                let triData = getTriData(line);
                if (triData){
                    info.tris.push(triData);
                }
                break;
            case "numweights":
                info.numWeights = getNumWeights(line);
                break;
            case "weight":
                let weightData = getWeightData(line);
                if (weightData){
                    info.weights.push(weightData);
                }
                break;
            default:
                //  ignore //
                break;

        }
    }
    return info;
}


function getFirstWord(line) {
    let contents = line.split(" ").filter(item=>item.length > 0);
    if (contents.length > 0){
        return contents[0]
    }
    return "";
}

function getImagePath(line) {
    let reg = /"(.*)"/g;
    let ret = reg.exec(line);
    if (ret.length > 1){
        return ret[1];
    }
    return ""
}

function getNumVerts(line) {
    let reg = /\d+/g;
    return parseInt(line.match(reg)[0]);
}

function getNumTris(line) {
    let reg = /\d+/g;
    return parseInt(line.match(reg)[0]);
}

function getNumWeights(line) {
    let reg = /\d+/g;
    return parseInt(line.match(reg)[0]);
}

function getVertData(line){
    let startIdx = line.indexOf('vert');
    let left = line.substring(startIdx + 4);
    let arr = left.split(" ").filter(item=>{
        return item.length > 0 && item != "(" && item != ")";
    });
    if (arr.length >= 5){
        return {
            vertexIdx:parseInt(arr[0]), // 顶点索引
            u:parseFloat(arr[1]),
            v:parseFloat(arr[2]),
            weightIdx:parseInt(arr[3]), // 第一个权重索引
            weightCount:parseInt(arr[4]), // 权重数量
        }
    } else{
        console.warn(`maybe parse line:${line} error!`)
        return null
    }
}

function getTriData(line) {
    let startIdx = line.indexOf('tri');
    let left = line.substring(startIdx + 3);
    let arr = left.split(" ").filter(item=>{
        return item.length > 0;
    });
    if (arr.length >= 4){
        return {
            triIdx:parseInt(arr[0]), // 三角形索引
            i0:parseInt(arr[1]), // 第一个顶点索引
            i1:parseInt(arr[2]),
            i2:parseInt(arr[3])
        }
    }else{
        console.warn(`maybe parse line:${line} error!`);
        return null
    }
}

function getWeightData(line) {
    let startIdx = line.indexOf('weight');
    let left = line.substring(startIdx + 6);
    let arr = left.split(" ").filter(item=>{
        return item.length > 0 && item != "(" && item != ")";
    });
    if (arr.length >= 6){
        return {
            weightIdx:parseInt(arr[0]), // 权重的索引
            boneIdx:parseInt(arr[1]), // 关联的骨骼
            weightValue:parseFloat(arr[2]), // 权重值
            offx:parseFloat(arr[3]),   // 权重平移的x分量
            offy:parseFloat(arr[4]),
            offz:parseFloat(arr[5]),
        }
    }else{
        console.warn(`maybe parse line:${line} error!`);
        return null
    }
}

let ret = parseMd5Mesh(lines.split("\n"));
console.log(JSON.stringify(ret));

// let line = `	weight 0 42 0.883179605 ( 2.3967983723 0.3203794658 -2.0581412315 )`;
// getWeightData(line);

// let line = `tri 0 2 1 0`
// getTriData(line);

// let line = `vert 0 ( 0.6911731958 0.0044568777 ) 5 1`;
// getVertData(line);

// let line = `numverts 14`;
// console.log(getNumVerts(line));

//
// let line1 = `shader "textures/monster/gob2.png"`;
// console.log(getImagePath(line1));
